﻿#include "tetrisgame.h"

TetrisGame::TetrisGame(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle(u8"Qt 俄罗斯方块");
    setFixedSize(800, 700);

    // 创建中央部件
    QWidget *centralWidget = new QWidget(this);
    QHBoxLayout *layout = new QHBoxLayout(centralWidget);

    // 创建游戏板
    board = new TetrisBoard;
    board->setFixedSize(300, 600);

    // 创建分数板
    scoreboard = new Scoreboard;

    // 创建游戏控制器
    controller = new GameController(board);

    // 设置布局
    layout->addWidget(board);
     layout->addWidget(scoreboard);
    centralWidget->setLayout(layout);
    setCentralWidget(centralWidget);

    // 连接信号槽
    connect(scoreboard, &Scoreboard::startGame, controller, &GameController::startGame);
    connect(scoreboard, &Scoreboard::pauseGame, controller, &GameController::pauseGame);

    connect(board, &TetrisBoard::scoreChanged, scoreboard, &Scoreboard::setScore);
    connect(board, &TetrisBoard::levelChanged, scoreboard, &Scoreboard::setLevel);
    connect(board, &TetrisBoard::linesRemovedChanged, scoreboard, &Scoreboard::setLinesRemoved);
    connect(board, &TetrisBoard::nextPieceChanged, [this]() {
        scoreboard->setNextPiece(board->getNextPiece());
    });
        connect(board, &TetrisBoard::gameOver, controller, &GameController::gameOver);

    // 创建菜单
    createMenu();

    // 设置样式
    centralWidget->setStyleSheet("background-color: #2c3e50;");

    // 初始化下一个方块显示
    scoreboard->setNextPiece(board->getNextPiece());
}

TetrisGame::~TetrisGame()
{
    delete controller;
}

void TetrisGame::createMenu()
{
    QMenu *gameMenu = menuBar()->addMenu(u8"游戏");

    QAction *newGameAction = gameMenu->addAction(u8"新游戏");
    connect(newGameAction, &QAction::triggered, controller, &GameController::startGame);

    QAction *pauseAction = gameMenu->addAction(u8"暂停");
    connect(pauseAction, &QAction::triggered, controller, &GameController::pauseGame);

    gameMenu->addSeparator();

    QAction *exitAction = gameMenu->addAction(u8"退出");
    connect(exitAction, &QAction::triggered, this, &QMainWindow::close);

    QMenu *helpMenu = menuBar()->addMenu(u8"帮助");

    QAction *aboutAction = helpMenu->addAction(u8"关于");
    connect(aboutAction, &QAction::triggered, []() {
        QMessageBox::about(nullptr, u8"关于俄罗斯方块",
                           u8"Qt 俄罗斯方块游戏\n"
                           u8"使用C++和Qt框架开发\n"
                           u8"版本 1.0");
    });

    QAction *controlsAction = helpMenu->addAction(u8"游戏控制");
    connect(controlsAction, &QAction::triggered, []() {
        QMessageBox::information(nullptr, u8"游戏控制",
                                 u8"方向键左: 左移方块\n"
                                 u8"方向键右: 右移方块\n"
                                 u8"方向键下: 加速下落\n"
                                 u8"方向键上: 旋转方块\n"
                                 u8"空格键: 直接落到底部\n"
                                 u8"P键: 暂停/继续游戏");
    });
}

